package top.lyc829.zzl_android.ui.base_detail;

import android.util.Pair;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.MyApp;
import top.lyc829.zzl_android.logic.bean.BaseBean;
import top.lyc829.zzl_android.logic.bean.BaseDetailBean;
import top.lyc829.zzl_android.logic.bean.MyTeamState;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.SonCommentBean;
import top.lyc829.zzl_android.logic.bean.SuperCommentBean;
import top.lyc829.zzl_android.logic.bean.TeamSimpleBean;
import top.lyc829.zzl_android.logic.bean.TravelsSimpleBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.ReplayType;
import top.lyc829.zzl_android.logic.util.Url;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

/**
 * @Author lyc
 * @create 2021-03-07 22:08
 */
public class BaseDetailViewModel extends ViewModel {

    private MutableLiveData<BaseDetailBean> baseDetailBeanMutableLiveData;
    private MutableLiveData<List<BaseBean>> recommendBeansLiveData;
    private MutableLiveData<List<TravelsSimpleBean>> travelsSimplesLiveData;
    private MutableLiveData<List<TeamSimpleBean>> teamSimplesLiveData;
    private MutableLiveData<List<SuperCommentBean>> superCommentsLiveData;
    private MutableLiveData<List<SonCommentBean>> sonCommentsLiveData;
    private MutableLiveData<Integer> attentionStateLiveData;

    private MutableLiveData<MyTeamState> myTeamStateLiveData;

    //子评论数量变化时，通知一级评论刷新被评论的数量
    private MutableLiveData<Pair<Integer, Integer>> sonCommentChangePositionLiveData;

    public MutableLiveData<Pair<Integer, Integer>> getSonCommentChangePositionLiveData() {
        if (sonCommentChangePositionLiveData == null) {
            sonCommentChangePositionLiveData = new MutableLiveData<>();
        }
        return sonCommentChangePositionLiveData;
    }

    public MutableLiveData<MyTeamState> getMyTeamStateLiveData() {
        if (myTeamStateLiveData == null) {
            myTeamStateLiveData = new MutableLiveData<>();
        }
        return myTeamStateLiveData;
    }

    // first: 评论类型（1级 n级） second 评论id
    private MutableLiveData<Pair<Integer, Integer>> commentDeletePositionLiveData;

    public MutableLiveData<Pair<Integer, Integer>> getCommentDeleteLiveData() {
        if (commentDeletePositionLiveData == null) {
            commentDeletePositionLiveData = new MutableLiveData<>();
            commentDeletePositionLiveData.setValue(new Pair<>(-1, -1));
        }
        return commentDeletePositionLiveData;
    }

    public MutableLiveData<Integer> getAttentionStateLiveData() {
        if (attentionStateLiveData == null) {
            attentionStateLiveData = new MutableLiveData<>();
        }
        return attentionStateLiveData;
    }



    public MutableLiveData<List<SonCommentBean>> getSonCommentsLiveData() {
        if (sonCommentsLiveData == null) {
            sonCommentsLiveData = new MutableLiveData<>();
        }
        return sonCommentsLiveData;
    }

    public MutableLiveData<List<SuperCommentBean>> getSuperCommentsLiveData() {
        if (superCommentsLiveData == null) {
            superCommentsLiveData = new MutableLiveData<>();
        }
        return superCommentsLiveData;
    }

    public MutableLiveData<List<TeamSimpleBean>> getTeamSimplesLiveData() {
        if (teamSimplesLiveData == null) {
            teamSimplesLiveData = new MutableLiveData<>();
        }
        return teamSimplesLiveData;
    }

    public MutableLiveData<List<TravelsSimpleBean>> getTravelsSimplesLiveData() {
        if (travelsSimplesLiveData == null) {
            travelsSimplesLiveData = new MutableLiveData<>();
        }
        return travelsSimplesLiveData;
    }

    public MutableLiveData<BaseDetailBean> getBaseDetailBeanMutableLiveData() {
        if (baseDetailBeanMutableLiveData == null) {
            baseDetailBeanMutableLiveData = new MutableLiveData<>();
        }
        return baseDetailBeanMutableLiveData;
    }

    public MutableLiveData<List<BaseBean>> getRecommendBeansLiveData() {
        if (recommendBeansLiveData == null) {
            recommendBeansLiveData = new MutableLiveData<>();
        }
        return recommendBeansLiveData;
    }

    /**
     * 根据基地id获取基地详细信息
     * @param id 基地id
     */
    public void getBaseDetailFromServer(int id) {
        HashMap<String, String> body = new HashMap<>();
        body.put("id", String.valueOf(id));
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());
        httpUtil.get(Url.GET_BASE_DETAIL, body, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<BaseDetailBean> resultBean = JsonUtilNew.fromJsonObject(resultStr, BaseDetailBean.class);
                if (resultBean.getCode().equals(200)) {
                    baseDetailBeanMutableLiveData.postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.SoutErr(e.getMessage());
            }
        });

    }

    public void getRecommendBases(int id) {
        HashMap<String, String> body = new HashMap<>();
        body.put("id", String.valueOf(id));
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());
        httpUtil.get(Url.GET_RECOMMEND_BASES, body, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<List<BaseBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, BaseBean.class);
                if (resultBean.getCode().equals(200)) {
                    getRecommendBeansLiveData().postValue(resultBean.getData());
                }

            }

            @Override
            public void failed(IOException e) {
                Utils.showToastSync("获取失败");
            }
        });
    }


    /**
     * 获取基地详情页的两个推荐游记
     * @param id 基地id
     * @param page 分页
     */
    public void getRecommendTravels(int id,int page) {
        HashMap<String, String> body = new HashMap<>();
        body.put("baseId", String.valueOf(id));
        body.put("page", String.valueOf(page));
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());
        httpUtil.get(Url.GET_RECOMMEND_TRAVELS, body, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<List<TravelsSimpleBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, TravelsSimpleBean.class);
                if (resultBean.getCode().equals(200)) {
                    getTravelsSimplesLiveData().postValue(resultBean.getData());
                }

            }

            @Override
            public void failed(IOException e) {
                Utils.showToastSync("获取失败");
            }
        });
    }


    public void getRecommendTeams(int id, int page) {
        HashMap<String, String> body = new HashMap<>();
        body.put("baseId", String.valueOf(id));
        body.put("page", String.valueOf(page));
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());
        httpUtil.get(Url.GET_TEAM_RECRUITS, body, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<List<TeamSimpleBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, TeamSimpleBean.class);
                if (resultBean.getCode().equals(200)) {
                    getTeamSimplesLiveData().postValue(resultBean.getData());
                }

            }

            @Override
            public void failed(IOException e) {
                Utils.showToastSync("获取失败");
            }
        });
    }

    /**
     * 获取一级评论
     * @param id
     * @param page
     */
    public void getSuperComments(int id, int page) {
        HashMap<String, String> body = new HashMap<>();
        body.put("type", String.valueOf(ReplayType.BASE));
        body.put("id", String.valueOf(id));
        body.put("page", String.valueOf(page));
        body.put("user_id", String.valueOf(Utils.getUserId()));
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());
        httpUtil.get(Url.GET_SUPER_COMMENTS, body, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                Utils.Sout(resultStr);
                ResultBean<List<SuperCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, SuperCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    getSuperCommentsLiveData().postValue(resultBean.getData());
                }

            }

            @Override
            public void failed(IOException e) {
                Utils.showToastSync("获取失败");
            }
        });
    }

    public void getSonComments(int commentId, int page) {
        HashMap<String, String> body = new HashMap<>();
        body.put("comment_id", String.valueOf(commentId));
        body.put("page", String.valueOf(page));
        body.put("user_id", String.valueOf(Utils.getUserId()));
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());
        httpUtil.get(Url.GET_SON_COMMENTS, body, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                Utils.Sout(resultStr);
                ResultBean<List<SonCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, SonCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    getSonCommentsLiveData().postValue(resultBean.getData());
                }

            }

            @Override
            public void failed(IOException e) {
                Utils.showToastSync("获取失败");
            }
        });
    }

    public void getAttentionState(int userId, int type, int typeId) {
        Network.getAttentionRelationship(userId, type, typeId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                TypeToken<ResultBean<Integer>> typeToken = new TypeToken<ResultBean<Integer>>() {};
                ResultBean<Integer> resultBean = new Gson().fromJson(resultStr, typeToken.getType());
                if (resultBean.getCode().equals(200)) {
                    getAttentionStateLiveData().postValue(1);
                } else if (resultBean.getCode().equals(201)) {
                    getAttentionStateLiveData().postValue(0);
                } else {
                    failed(new IOException());
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout("获取关注关系失败");
            }
        });
    }


    public void getMyTeamIdFromServer(int userId) {
        Network.getIsInTeam(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<MyTeamState> resultBean = JsonUtilNew.fromJsonObject(resStr, MyTeamState.class);
                if (resultBean.getCode().equals(200)) {
                    if (resultBean.getData() == null) {
                        getMyTeamStateLiveData().postValue(null);
                    }
                    getMyTeamStateLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }



}
